package com.android.microweather;

import java.io.FileNotFoundException;
import java.util.Calendar;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

class ForecastListView extends LinearLayout {

	public ForecastListView(Context context, ForecastedDay day) {
		super(context);
		
		setOrientation(LinearLayout.HORIZONTAL);
		
		ImageView forecastImage = new ImageView(context);
		try {
			forecastImage.setImageBitmap(Utils.createBitmap(context, day));
		} catch (FileNotFoundException e) {
			// Fallback plan: assign N/A image if there's nothing else to show.
			forecastImage.setImageResource(R.drawable.not_available);
		}
		LinearLayout.LayoutParams forecastImageParams =
			new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		forecastImageParams.setMargins(1, 1, 1, 1);
		addView(forecastImage, forecastImageParams);
		
		TextView cityName = new TextView(context);
		cityName.setText(day.getTitle());
		cityName.setTextSize(14f);
		cityName.setTextColor(Color.WHITE);
		LinearLayout.LayoutParams cityNameParams =
			new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		addView(cityName, cityNameParams);
	}	
}

public class ForecastListAdapter extends BaseAdapter {

	private Context	context;
	private City 	city;
	private int 	daysInPast;	// Number of days that are already in the past, hence not to be displayed
	
	public ForecastListAdapter(Context context, City city) {
		super();
		this.context = context;
		this.city = city;
		
		/*FAQCalendar cal = new FAQCalendar(city.getLastUpdated().getTime());
		this.daysInPast = (int) cal.diffDayPeriods(Calendar.getInstance());*/
		Calendar calStart = Calendar.getInstance();
		calStart.setTime(city.getLastUpdated());
		calStart.set(Calendar.HOUR_OF_DAY, 0);
		calStart.set(Calendar.MINUTE, 0);
		calStart.set(Calendar.SECOND, 0);
		calStart.set(Calendar.MILLISECOND, 0);

		Calendar calEnd = Calendar.getInstance();
		calEnd.set(Calendar.HOUR_OF_DAY, 0);
		calEnd.set(Calendar.MINUTE, 0);
		calEnd.set(Calendar.SECOND, 0);
		calEnd.set(Calendar.MILLISECOND, 0);
		
		FAQCalendar calCulator = new FAQCalendar(calStart.getTime().getTime());
		this.daysInPast = (int) calCulator.diffDayPeriods(calEnd);
	}

	@Override
	public int getCount() {
		int count = city.getDays().size() - daysInPast;
		return count < 0 ? 0 : count;
	}

	@Override
	public Object getItem(int arg0) {
		return city.getDays().get(arg0);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ForecastedDay day = city.getDays().get(position + daysInPast);
		return new ForecastListView(context, day);
	}
}
